package com.shadt.sms.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.shadt.core.model.Json;
import com.shadt.core.util.ConfigUtil;
import com.shadt.sms.SendUtil;
import com.shadt.sms.dao.MessageInfoDao;
import com.shadt.sms.entity.MessageInfo;
import com.shadt.sms.service.MessageInfoService;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

/**
 * 短信业务层实现类
 * 
 * @author SongR
 * 
 */
@Component
@Service
public class MessageInfoServiceImpl implements MessageInfoService {
	@Autowired
	MessageInfoDao mDao;
	Logger log = Logger.getLogger(this.getClass());
	
	public void send() {
		List<MessageInfo> mList = mDao.getAll("select * from sp_message_info t where t.mstatus=0 and t.MSGTYPE=0");
		log.info("成功获取"+mList.size()+"条未发送信息！");
		String url = ConfigUtil.get("ali.sms.url");
		String appkey = ConfigUtil.get("ali.sms.appkey");
		String secret=ConfigUtil.get("ali.sms.secret");
		int i = 0;
		for (MessageInfo m : mList) {
			TaobaoClient client = new DefaultTaobaoClient(url,appkey,secret,"json");
			AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
			req.setExtend( "" );
			req.setSmsType( "normal" );
			req.setSmsFreeSignName(ConfigUtil.get("ali.sms.signname"));
			req.setSmsParamString( "{code:'"+m.getBody()+"'}" );
			req.setRecNum(m.getmPhone());
			req.setSmsTemplateCode(ConfigUtil.get("ali.sms.template"));
			try {
				AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
				JSONObject j = JSONObject.parseObject(rsp.getBody());
				log.info(rsp.getBody());
				String uSql="";
				if(j.get("alibaba_aliqin_fc_sms_num_send_response")!=null&&!"".equals(j.get("alibaba_aliqin_fc_sms_num_send_response"))){
					uSql = "update sp_message_info set mstatus=1, smsresult='"+j.get("alibaba_aliqin_fc_sms_num_send_response")+"' where seqno='"+m.getSeqNo()+"'";
				}else{
					uSql = "update sp_message_info set mstatus=-1, smsresult='"+j.get("error_response")+"' where seqno='"+m.getSeqNo()+"'";
				}
				mDao.updateMessage(uSql);
				i++;
			} catch (ApiException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		log.info("成功发送"+i+"条信息！");
	}
	
	

	public void sendMessage() {
		List<MessageInfo> mList = mDao.getAll("select * from sp_message_info t where t.mstatus=0");
		log.info("成功获取"+mList.size()+"条未发送信息！");
		String url = ConfigUtil.get("url");
		int i = 0;
		for (MessageInfo m : mList) {
			String param="";
			try {
				param = "action=send&"+"userid=" + ConfigUtil.get("userid")+"&account="+ConfigUtil.get("account")+"&password="+ConfigUtil.get("password")+"&mobile="+m.getmPhone()+"&content="+URLEncoder.encode(m.getBody(),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} 
			Json j = SendUtil.parse(SendUtil.request(url, param));
			String uSql="";
			if(j.isSuccess()){
				uSql = "update sp_message_info set mstatus=1, smsresult='"+j.getMsg()+"' where seqno='"+m.getSeqNo()+"'";
			}else{
				uSql = "update sp_message_info set mstatus=-1, smsresult='"+j.getMsg()+"' where seqno='"+m.getSeqNo()+"'";
			}
			mDao.updateMessage(uSql);
			i++;
		}
		log.info("成功发送"+i+"条信息！");
	}
}
